﻿using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows;
using System.Collections;

namespace IMap
{
    public enum CacheType : int
    {
        Server,
        Cache,
        CacheAndServer,
    }
    public enum LayerType : int
    {
        Map = 1,
        Trans=2,
        Grid = 3,
        Marker = 4,
        Radar = 5,
    }

    public class OverLayer : List<UIElement>
    {
        public MapContainer Container { get; set; }
        public LayerType Layer { get; set; }

        private string layerName;
        public string LayerName { get { return layerName; } set { layerName = value; } }

        private int zindex;
        public int ZIndex { get { return zindex; } set { zindex = value; } }

        public OverLayer(MapContainer container)
        {
            this.Container = container;
            this.LayerName = "";
            this.Layer = new LayerType();//默认值为0
            this.ZIndex = 0;
        }

        public OverLayer(MapContainer container, LayerType layer)
        {
            this.Container = container;
            this.LayerName = layer.ToString();
            this.Layer = layer;
            this.ZIndex = (int)layer;
        }

        public void Add(UIElement item, double x, double y)
        {
            Canvas.SetLeft(item, x);
            Canvas.SetTop(item, y);
            Canvas.SetZIndex(item, zindex);
            this.Container.Children.Add(item);
            base.Add(item);
        }

        public new void Remove(UIElement item)
        {
            this.Container.Children.Remove(item);
            base.Remove(item);
        }

        public void RemoveAll()
        {
            int length = this.Container.Children.Count;
            for (int i = 0; i < length; i++)
            {
                UIElement item = this.Container.Children[i];
                if (Canvas.GetZIndex(item) == this.ZIndex)
                {
                    this.Container.Children.Remove(item);
                    //注意此处集合已经更改
                    i--;
                    length--;
                }                
            }
            base.Clear();
        }

        public override string ToString()
        {
            return this.Layer.ToString();
        }

        //public List<T> GetChildObjects<T>(DependencyObject obj, Type typename) where T : FrameworkElement
        //{
        //    DependencyObject child = null;
        //    List<T> childList = new List<T>();

        //    for (int i = 0; i <= VisualTreeHelper.GetChildrenCount(obj) - 1; i++)
        //    {
        //        child = VisualTreeHelper.GetChild(obj, i);

        //        if (child is T && (((T)child).GetType() == typename))
        //        {
        //            childList.Add((T)child);
        //        }
        //        childList.AddRange(GetChildObjects<T>(child, typename));
        //    }
        //    return childList;
        //}
    }

    public class LayersManager : List<OverLayer>
    {
        public MapContainer Container { get; set; }
        public Hashtable controlsPostions_Hash { get; set; }

        public LayersManager(MapContainer container)
        {
            this.Container = container;
        }

        public bool Contains(LayerType layer)
        {
            return this.Exists(delegate(OverLayer item) {return item.Layer.Equals(layer); });
        }

        public void Remove(LayerType layer)
        {
            //匿名委托方式，注意调用该方法之前，应检查值是否存在
            this.Items(layer).RemoveAll();
            //此方法调用后，图层名称任然存在
        }

        //清除空图层
        public void Purge() 
        {
            int length = base.Count;
            for (int i = 0; i < length; i++)
            {
                OverLayer item = this[i];
                if (item.Count == 0)
                {
                    base.Remove(item);
                    i--;
                    length--;
                }                
            }        
        }

        public OverLayer Items(LayerType layer)
        {
            //匿名委托方式
            return this.Find(delegate(OverLayer item)
            { return item.Layer.Equals(layer); });

            //遍历方式
            //OverLayer overlayer = null;
            //foreach (OverLayer item in this)
            //{
            //    if (item.LayerName == layer.ToString())
            //    {
            //        overlayer = item;
            //    }
            //}
            //return overlayer;
        }

        public void Add(LayerType layer)
        {
            OverLayer overlayer = new OverLayer(this.Container, layer);
            this.Add(overlayer);
        }

        public new void Add(OverLayer item)
        {
            item.Container = this.Container;
            base.Add(item);
        }

        public Hashtable GetHash()
        {
            controlsPostions_Hash = new Hashtable();
            PixelPoint posit = new PixelPoint();
            for (int i = 0; i < this.Container.Children.Count; i++)
            {
                if (Canvas.GetZIndex(this.Container.Children[i]) >(int) LayerType.Grid)
                {
                    posit = new PixelPoint(Canvas.GetLeft(this.Container.Children[i]), Canvas.GetTop(this.Container.Children[i]));
                    controlsPostions_Hash.Add(this.Container.Children[i], posit);
                }
            }
            return controlsPostions_Hash;
        }       

        public void Offset(double deltaX, double deltaY)
        {
            PixelPoint beforePosit, afterPosit;
            PixelPoint deltaPoint = new PixelPoint(deltaX, deltaY);
            foreach (DictionaryEntry item in controlsPostions_Hash)
            {
                beforePosit = (PixelPoint)item.Value;
                afterPosit = beforePosit + deltaPoint;
                Canvas.SetLeft((UIElement)item.Key, afterPosit.X);
                Canvas.SetTop((UIElement)item.Key, afterPosit.Y);
            }
        }
    }

}



